@echo off
echo Starting MariaDB service...

REM Check if MariaDB service exists
sc query "MariaDB" > nul
if %ERRORLEVEL% EQU 0 (
    REM Service exists, check if it's running
    sc query "MariaDB" | findstr "RUNNING" > nul
    if %ERRORLEVEL% EQU 0 (
        echo MariaDB service is already running
    ) else (
        echo Starting MariaDB service...
        net start MariaDB
        if %ERRORLEVEL% EQU 0 (
            echo MariaDB service started successfully
        ) else (
            echo Failed to start MariaDB service, error code: %ERRORLEVEL%
        )
    )
) else (
    REM Service doesn't exist, try to start from installation directory
    echo MariaDB service is not installed or has a different name
    
    REM Try to start from default location
    if exist ".\MariaDB 10.5\bin\mysqld.exe" (
        echo Starting MariaDB from current directory...
        start /b "MariaDB" ".\MariaDB 10.5\bin\mysqld.exe" --console
        echo MariaDB started, please do not close the command window
    ) else (
        echo MariaDB executable not found, please ensure MariaDB is properly installed
        echo or start MariaDB service manually
    )
)

echo.
echo Waiting 5 seconds to ensure MariaDB is fully started...
timeout /t 5 /nobreak > nul

echo.
echo You can now start the FinData application
echo. 